﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dtSearch32Lib
{
    public class SearchReturnPaging
    {
        public int StartIndex = 0;
        public int EndIndex = 0;
        public int TotalPage = 0;
        public int NoOfVisiblePage = 0;
        public int TotalSegment = 0;
        public string PagingText = "";
    }

    public class PagingUtilities
    {
        public static SearchReturnPaging GetPaging(
            int MaxItems,
            int CurrentPage,
            int ItemsPerRow,
            int NoOfVisiblePage,
            string JumpJavaScript)
        {
            return GetPaging(MaxItems, CurrentPage, ItemsPerRow, NoOfVisiblePage, JumpJavaScript, 1);
        }



        public static SearchReturnPaging GetPaging(
            int MaxItems,
            int CurrentPage,
            int ItemsPerRow,
            int NoOfVisiblePage,
            string JumpJavaScript,
            int ShowInfo)
        {
            SearchReturnPaging oReturnPaging = new SearchReturnPaging();


            if (NoOfVisiblePage == 0) NoOfVisiblePage = 5;
            if (ItemsPerRow == 0) ItemsPerRow = 25;
            int TotalPage = MaxItems / ItemsPerRow;
            if (MaxItems % ItemsPerRow != 0) TotalPage++;

            if (TotalPage <= 1)
            {
                oReturnPaging.StartIndex = 0;
                oReturnPaging.EndIndex = MaxItems;
                oReturnPaging.TotalPage = 1;
                oReturnPaging.NoOfVisiblePage = NoOfVisiblePage;
                oReturnPaging.TotalSegment = 0;
                if (ShowInfo == 1)
                {
                    oReturnPaging.PagingText =
                        "<div class=\"CssRecordFound\">Tổng số có " + MaxItems.ToString("#,#0") + " bản ghi tìm thấy</div>\r\n";
                }
                return oReturnPaging;
            }

            oReturnPaging.StartIndex = ItemsPerRow * CurrentPage;
            oReturnPaging.EndIndex = oReturnPaging.StartIndex + ItemsPerRow;
            if (oReturnPaging.EndIndex > MaxItems) oReturnPaging.EndIndex = MaxItems;

            int TotalSegment = TotalPage / NoOfVisiblePage;
            if (TotalPage % NoOfVisiblePage != 0) TotalSegment++;
            //doan nay tinh xem no nam o segment may
            int CurrentSegment = 0;
            for (int iSegment = 0; iSegment < TotalSegment; iSegment++)
            {
                int FromPage = iSegment * NoOfVisiblePage;
                int EndPage = FromPage + NoOfVisiblePage;
                if (EndPage > TotalPage) EndPage = TotalPage;
                if (CurrentPage >= FromPage && CurrentPage < EndPage)
                {
                    CurrentSegment = iSegment;
                    break;
                }
            }

            int StartPageIndex = CurrentSegment * NoOfVisiblePage;
            int EndPageIndex = StartPageIndex + NoOfVisiblePage;
            if (EndPageIndex > TotalPage) EndPageIndex = TotalPage;
            string Html = "";
            if (ShowInfo == 1)
            {
                Html +=
                    "<div class=PageTitle style=\"text-align:right;\">Tổng số có " + MaxItems.ToString("#,#0") + " bản ghi tìm thấy</div>\r\n";
            }
            string Id = System.Guid.NewGuid().ToString().Substring(0, 8);
            Html +=
                    "<script language=javascript>\r\n"+
                    "   function ScriptKeyPressNextPage"+Id+"(e)\r\n"+
                    "   {\r\n"+
                    "       if (e.keyCode == 13) {\r\n"+
                    "           var tb = document.getElementById(\"txtJumpPageIndex"+Id+"\");\r\n"+
                    "           cPage=tb.value;\r\n"+
                    "           if(isNaN(parseInt(cPage)))\r\n"+
                    "           {\r\n"+
                    "               alert('Trang không hợp lệ. Xin vui lòng kiểm tra lại');\r\n"+
                    //"               tb.selectAll();\r\n"+
                    "               tb.focus();\r\n"+
                    "           }\r\n"+
                    "           else\r\n"+
                    "           {\r\n"+
                    "               Ids=document.getElementById('txtMaxPageIndex"+Id+"').value;\r\n"+
                    "               MaxPage=parseInt(Ids);\r\n" +
                    "               if(cPage>MaxPage || cPage<1)\r\n" +
                    "               {\r\n"+
                    "                   alert('Trang nhập không hợp lệ. Xin vui lòng nhập từ 1 đến '+MaxPage+' !');\r\n"+
                    "                   return;\r\n"+
                    "               }\r\n"+
                    "               "+JumpJavaScript + "(parseInt(cPage)-1);\r\n"+
                    "           }\r\n"+
                    "           return false;\r\n"+
                    "       }\r\n"+
                    "   }\r\n"+
                    "   function ScriptButtonClickNextPage" + Id + "()\r\n" +
                    "   {\r\n" +
                    "           var tb = document.getElementById(\"txtJumpPageIndex" + Id + "\");\r\n" +
                    "           cPage=tb.value;\r\n" +
                    "           if(isNaN(parseInt(cPage)))\r\n" +
                    "           {\r\n" +
                    "               alert('Trang không hợp lệ. Xin vui lòng kiểm tra lại');\r\n" +
                    "               tb.focus();\r\n" +
                    "           }\r\n" +
                    "           else\r\n" +
                    "           {\r\n" +
                    "               Ids=document.getElementById('txtMaxPageIndex" + Id + "').value;\r\n" +
                    "               MaxPage=parseInt(Ids);\r\n" +
                    "               if(cPage>MaxPage || cPage<1)\r\n" +
                    "               {\r\n" +
                    "                   alert('Trang nhập không hợp lệ. Xin vui lòng nhập từ 1 đến '+MaxPage+' !');\r\n" +
                    "                   return;\r\n" +
                    "               }\r\n" +
                    "               " + JumpJavaScript + "(parseInt(cPage)-1);\r\n" +
                    "           }\r\n" +
                    "   }\r\n" +
                    "</script>\r\n"+
                    "<input type=hidden id=\"txtMaxPageIndex" + Id + "\" value=\"" + TotalPage + "\">\r\n" +
                    "<div class=\"pagination\">\r\n" +
                    "<ul>\r\n"+
                    "<li><a alt=\"Trang đầu\" title=\"Trang đầu\" href=\"javascript:" + JumpJavaScript + "(0);\">««</a></li>";

            if (CurrentPage > 0)
            {
                Html += "<li><a alt=\"Trang trước\" title=\"Trang trước\" href=\"javascript:" + JumpJavaScript + "(" + (CurrentPage - 1) + ");\">«</a></li>";
            }
            else
            {
                Html += "<li><a>«</a></li>";
            }
            for (int iIndex = StartPageIndex; iIndex < EndPageIndex; iIndex++)
            {
                if (iIndex != CurrentPage)
                {
                    Html += "<li><a href=\"javascript:" + JumpJavaScript + "(" + iIndex + ");\">" + (iIndex + 1) + "</a></li>";
                }
                else
                {
                    Html += "<li class=\"disabled\"><a href=\"javascript:" + JumpJavaScript + "(" + iIndex + ");\">" + (iIndex + 1) + "</a></li>";
                }
            }
            if (CurrentPage < TotalPage - 1)
            {
                Html += "<li><a alt=\"Trang sau\" title=\"Trang sau\" href=\"javascript:" + JumpJavaScript + "(" + (CurrentPage + 1) + ");\">»</a></li>";
            }
            else
            {
                Html += "<li><a>»</a></li>";
            }
            Html += "<li><a alt=\"Trang cuối\" title=\"Trang cuối\" href=\"javascript:" + JumpJavaScript + "(" + (TotalPage - 1) + ");\">»»</a></li>";
            Html += "<li style=\"width:200px;border:none;\"><span>Chuyển đến trang</span>: <input textbox=textbox class=textbox style=\"width:30px;text-align:right;\" onkeypress=\"return ScriptKeyPressNextPage" + Id + "(event)\" id=\"txtJumpPageIndex" + Id + "\" value=\"" + (CurrentPage + 1) + "\"><input type=button class=button value=\"Go\" onclick=\"javascript:ScriptButtonClickNextPage"+Id+"();\"></li>" +
                  "</ul>" +
                  "</div>\r\n";
            oReturnPaging.PagingText = Html;
            oReturnPaging.TotalPage = TotalPage;
            oReturnPaging.NoOfVisiblePage = NoOfVisiblePage;
            oReturnPaging.TotalSegment = TotalSegment;
            return oReturnPaging;
        }
    }
}
